package interfaz;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
/*
 * iPictureMenu.java
 *
 * Created on 22 de febrero de 2008, 22:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author victor
 */
public class iPictureMenu extends iWidget {
    
    private int w2 = 100;
    private int h2 = 100; //ancho*alto eltos non activos
    private int w1 = 130;
    private int h1 = 130; //ancho*alto eltos case activos
    private int w = 180;
    private int h = 180; //ancho*alto eltos  activos
    private ArrayList pictureList = new ArrayList();
    private int activeItem = 0;
    private Canvas canvas;
    private boolean horizontal = false; //orientacion na pantalla
    private int border = 0;
    
    
    /** Creates a new instance of iPictureMenu */
    public iPictureMenu(int x, int y) {
        super(x,y);
    }
    public void setBorder(int border) {
        this.border = border;
    }
    public int getBorder() {
        return border;
    }
    public void setW1(int width) {
        this.w1 = width;
    }
    public int getW1() {
        return w1;
    }
    public void setH1(int height) {
        this.h1 = height;
    }
    public int getH1() {
        return h1;
    }
    public void setW2(int width) {
        this.w2 = width;
    }
    public int getW2() {
        return w2;
    }
    public void setH2(int height) {
        this.h2 = height;
    }
    public int getH2() {
        return h2;
    }
    public void setW(int width) {
        this.w = width;
    }
    public int getW() {
        return w;
    }
    public void setH(int height) {
        this.h = height;
    }
    public int getH() {
        return h;
    }
    public void setHorizontal(boolean status) {
        this.horizontal = status;
    }
    public boolean getHorizontal() {
        return this.horizontal;
    }
    public void setCanvas(Canvas canvas) {
        this.canvas = canvas;
    }
    
    public void addPicture(String path) {
        iPicture picture = new iPicture(0,0);
        picture.setImage(path);
        picture.setBorder(getBorder());
        picture.setBorderColor(Color.WHITE);
        picture.setCanvas(canvas);
        picture.setX(getX());
        picture.setY(getY());
        picture.setWidth(w2);
        picture.setHeight(h2);
        pictureList.add(picture);
    }
    
    public void delPicture(int index) {
        pictureList.remove(index);
    }
    
    public iPicture getPicture(int index) {
        return (iPicture) pictureList.get(index);
    }
    
    public int size() {
        return pictureList.size();
    }
    
    public void setActiveItem(int item) {
        this.activeItem = item;
    }
    
    public void decreaseActiveItem() {
        if (getActiveItem() > 0) setActiveItem(getActiveItem()-1);
    }
    
    public void increaseActiveItem() {
        if (getActiveItem() < size()-1) setActiveItem(getActiveItem()+1);
    }
    
    public int getActiveItem() {
        return this.activeItem;
    }

    public Canvas getCanvas() {
        return canvas;
    }
    
    public int getDisplayableMenuSize() {
        //num de eltos inactivos representables
        int DisplayableMenuSize;
        if (!horizontal) {
            DisplayableMenuSize = getHeight()-getH()-2*getH1()-3*10;
            DisplayableMenuSize = DisplayableMenuSize/(getH2()+10);
        } else {
            DisplayableMenuSize = getWidth()-getW()-2*getW1()-3*10;
            /*altura disponhible despois de considerar 3 eltos fixos,
             10px separacion */
            DisplayableMenuSize = DisplayableMenuSize/(getW2()+10);
        }
        if (DisplayableMenuSize+3 > size()) return size();
        return DisplayableMenuSize+3;
    }
    
    public int getMinIndex() { //devolve a posicion do elto que se ve primeiro
        //na pantalla
        if (getActiveItem() <= getDisplayableMenuSize()/2) return 0;
        else if (getActiveItem() >= size()-getDisplayableMenuSize()/2)
            return (size()-getDisplayableMenuSize());
        else return getActiveItem()-getDisplayableMenuSize()/2;
    }
    
    public int getMaxIndex() {
        return getMinIndex() + getDisplayableMenuSize();
    }
    
    
    public void paint(Graphics g) {
        iPicture picture;
        
        for (int i=0;i < getDisplayableMenuSize();i++) {
            picture = (iPicture) getPicture(getMinIndex()+i);
            picture.setHeight(getH2());
            picture.setWidth(getW2());
            if ((i+getMinIndex() <= getActiveItem()-1)) {
                if (!horizontal) picture.setY(getY()+i*(getH2()+10));
                else picture.setX(getX()+i*(getW2()+10));
                //avanzamos posicion sen consideracions
            }
            if (i+getMinIndex() == getActiveItem()+1) {
                //estamos no elto case activo despois do resaltado
                if (!horizontal) picture.setY(
                        getY()+(i-2)*(getH2()+10)+getH1()+10+getH()+10);
                else picture.setX(
                        getX()+(i-2)*(getW2()+10)+getW1()+10+getW()+10);
            }
            if (i+getMinIndex() > getActiveItem()+1) {
                //volvemos a estar tratando eltos de tamanho normal
                if (!horizontal)
                    picture.setY(
                            getY()+(i-3)*(getH2()+10)+2*(getH1()+10)+getH()+10);
                else picture.setX(
                        getX()+(i-3)*(getW2()+10)+2*(getW1()+10)+getW()+10);
                /* consideramos todolos eltos de tamanho normal
                 + o elto resaltado + 2 eltos de tamanho case activo */
            }
            if (i+getMinIndex() == getActiveItem()) {
                if (!horizontal)
                    picture.setY(getY()+(i-1)*(getH2()+10)+getH1()+10);
                else
                    picture.setX(getX()+(i-1)*(getW2()+10)+getW1()+10);
                /*consideramos todolos eltos de tamanho normal
                 *+ un elto de tamanho case activo */
                picture.setWidth(w);
                picture.setHeight(h);
            }
            if ((i+getMinIndex() == getActiveItem()-1)
            || (i+getMinIndex() == getActiveItem()+1)) {
                //actualizamos diemsions
                picture.setWidth(w1);
                picture.setHeight(h1);
            }
            
            picture.paint(g);
        }
    }
    
    
    
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP: {
                decreaseActiveItem();
                break;
            }
            case KeyEvent.VK_DOWN: {
                increaseActiveItem();
                break;
            }
        }
    }
}
